package com.yueka.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yueka.mapper.CourseMapper;
import com.yueka.mapper.CourseRecordMapper;
import com.yueka.mapper.CourseStudentMapper;
import com.yueka.mapper.StudentMapper;
import com.yueka.pojo.PageResult;
import com.yueka.pojo.entity.course.Course;
import com.yueka.pojo.entity.course.CourseRecord;
import com.yueka.service.CourseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseRecordServiceImpl implements CourseRecordService {
    @Autowired
    private CourseRecordMapper courseRecordMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CourseStudentMapper courseStudentMapper;
    @Autowired
    private CourseMapper courseMapper;

    @Override
    @Transactional
    public PageResult<CourseRecord> list(String schoolTime, String status, Integer page, Integer pageSize) {
        Page<CourseRecord> list = courseRecordMapper.getList(new Page<>(page, pageSize), schoolTime, status);
        return new PageResult<>(list.getTotal(), list.getRecords());
    }

    @Override
    public PageResult<CourseRecord> getByStudentId(Integer studentId, LocalDateTime start, LocalDateTime end, String crType, Integer page, Integer pageSize) {
        Page<CourseRecord> list = courseRecordMapper.getByStudentId(new Page<>(page, pageSize), studentId, start, end, crType);
        return new PageResult<>(list.getTotal(), list.getRecords());
    }

    @Override
    public PageResult<CourseRecord> getByTeacherId(Integer teacherId, LocalDateTime start, LocalDateTime end, Integer page, Integer pageSize) {
        Page<CourseRecord> list = courseRecordMapper.getByTeacherId(new Page<>(page, pageSize), teacherId, start, end);
        return new PageResult<>(list.getTotal(), list.getRecords());
    }

    @Override
    @Transactional
    public void update(CourseRecord courseRecord) {
        courseRecordMapper.update(courseRecord);
        courseMapper.finishCourse(courseRecord.getCourseId());
    }


    @Override
    public List<Course> openCourse(LocalDateTime date) {
        LocalDateTime start = date.withHour(0).withMinute(0).withSecond(0);
        LocalDateTime end = date.withHour(23).withMinute(59).withSecond(59);
        return courseMapper.openCourse(start, end);
    }

    @Override
    public void subscribe(Integer studentId, Integer courseId) {
        Integer i = courseStudentMapper.isSubscribe(studentId, courseId);
        if (i > 0) {
            throw new RuntimeException("您已经订阅过了");
        }
        courseStudentMapper.subscribe(studentId, courseId);
    }

    @Override
    @Transactional
    public void insert(CourseRecord courseRecord) {

        courseRecordMapper.insert(courseRecord);
        courseMapper.finishCourse(courseRecord.getCourseId());
    }

    @Override
    public void deleteById(CourseRecord courseRecord) {
        courseRecordMapper.deleteById(courseRecord.getId());
        courseMapper.cancelCourse(courseRecord.getCourseId());
    }
}
